package com.caifu.controller;


import com.caifu.pojo.CDevice;
import com.caifu.pojo.COrg;
import com.caifu.pojo.SysUser;
import com.caifu.service.CDeviceService;
import com.caifu.service.COrgDeptService;
import com.caifu.service.COrgService;
import com.caifu.util.Utils;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备信息表 前端控制器
 * </p>
 *
 * @author tww
 * @since 2020-12-10
 */
@Controller
@RequestMapping("center/device")
public class CDeviceController extends AbstractController{
    final static Logger logger = LoggerFactory.getLogger(CDeviceController.class);

    @Resource
    private CDeviceService deviceService;
    @Resource
    private COrgService orgService;
    @Resource
    private COrgDeptService deptService;

    /**
     * 设备列表
     * @param model
     * @return
     * @throws Exception
     */
    @GetMapping(value = "list")
    public String list(ModelMap model) throws Exception{
        SysUser user = getUser();
        if("0".equals(user.getUserType())){
            return "view/device/list";
        } else if("1".equals(user.getUserType())) {
            return "view/device/org_list";
        } else {
            return "view/device/dept_list";
        }
    }

    /**
     *
     * @param currentPage
     * @param nums
     * @param deviceName
     * @param state
     * @return
     * @throws Exception
     */
    @GetMapping(value = "deviceList")
    @ResponseBody
    public JSONObject deviceList(int currentPage, int nums, String deviceName, String state) throws Exception {
        SysUser user = getUser();
        JSONObject obj = new JSONObject();
        try {
            Map map = new HashMap();
            map.put("minNum", (currentPage-1)*nums);
            map.put("maxNum", nums);
            if(Utils.isNotNull(deviceName)){
                map.put("deviceName", "%"+deviceName+"%");
            }
            map.put("state", state);
            int total = 0;
            List<Map<String, Object>> list = new ArrayList<>();
            if("0".equals(user.getUserType())) {
                //平台设备列表
                list = deviceService.listDevice(map);
                total = deviceService.countDevice(map);
            } else if("1".equals(user.getUserType())){
                //机构设备列表
                map.put("orgId", user.getRelativeId());
                list = deviceService.listOrgDevice(map);
                total = deviceService.countOrgDevice(map);
            } else{
                //部门设备列表
                map.put("deptId", user.getRelativeId());
                list = deviceService.listDeptDevice(map);
                total = deviceService.countDeptDevice(map);
            }
            int i = (currentPage-1)*nums +1;
            for (Map<String, Object> maps : list) {
                maps.put("num",i);
                i++;
            }
            obj.put("data", list);
            obj.put("count", total);
            obj.put("code",0);
            obj.put("msg","");
        } catch (Exception e) {
            e.printStackTrace();
            obj.put("code",301);
            obj.put("msg","查询异常");
        }
        return obj;
    }



    /**
     * 设备添加
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/add")
    public String add(ModelMap model) throws Exception{
        try {
            SysUser user = getUser();
            model.put("user", user);
            return "view/device/add";
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/save")
    @ResponseBody
    public JSONObject save(CDevice device) throws Exception {
        SysUser user = getUser();
        JSONObject obj = new JSONObject();
        int code = 200;
        String msg = "保存成功！";
        try {
            Map map = deviceService.addDeviceInfo(device);
            code = Integer.valueOf(map.get("code").toString());
            msg = map.get("msg").toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        obj.put("code",code);
        obj.put("msg",msg);
        return obj;
    }


    /**
     * 设备编辑
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/edit")
    public String edit(ModelMap model, Integer id) throws Exception{
        try {
            CDevice device = deviceService.getDeviceInfo(id);

            model.put("device", device);
            return "view/device/edit";
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //获取设备关联机构
    @PostMapping("/orgList")
    @ResponseBody
    public JSONObject orgList(int id) throws Exception {
        SysUser user = getUser();
        JSONObject obj = new JSONObject();
        int code = 200;
        String msg = "查询成功！";
        try {
            Map map = new HashMap();
            map.put("deviceId",id);
            List<Map<String, Object>> oList = deviceService.listOrgDevice(map);
            obj.put("oList", oList);

            List<COrg> list = orgService.listOrg();
            obj.put("list", list);
        } catch (Exception e) {
            e.printStackTrace();
            code = 301;
            msg ="查询异常";
        }
        obj.put("code",code);
        obj.put("msg",msg);
        return obj;
    }



    @PostMapping("/update")
    @ResponseBody
    public JSONObject update(CDevice device) throws Exception {
        SysUser user = getUser();
        JSONObject obj = new JSONObject();
        int code = 200;
        String msg = "修改成功！";
        try {
            Map map = deviceService.editDeviceInfo(device);
            code = Integer.valueOf(map.get("code").toString());
            msg = map.get("msg").toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        obj.put("code",code);
        obj.put("msg",msg);
        return obj;
    }

    @PostMapping("/delete")
    @ResponseBody
    public JSONObject delete(int id) throws Exception {
        SysUser user = getUser();
        JSONObject obj = new JSONObject();
        int code = 200;
        String msg = "删除成功！";
        try {
            Map map = deviceService.delDeviceInfo(id);
            code = Integer.valueOf(map.get("code").toString());
            msg = map.get("msg").toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        obj.put("code",code);
        obj.put("msg",msg);
        return obj;
    }




    /**
     * 机构列表
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/getOrgList")
    @ResponseBody
    public JSONObject getOrgList() throws Exception {
        SysUser user = getUser();
        JSONObject obj = new JSONObject();
        int code = 200;
        String msg = "查询成功！";
        try {
            List<COrg> list = orgService.listOrg();
            obj.put("list",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        obj.put("code",code);
        obj.put("msg",msg);
        return obj;
    }

    /**
     * 部门列表
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/deptList")
    @ResponseBody
    public JSONObject deptList() throws Exception {
        SysUser user = getUser();
        JSONObject obj = new JSONObject();
        int code = 200;
        String msg = "查询成功！";
        try {
            List<COrg> list = deptService.listDept(user.getRelativeId());
            obj.put("list",list);
        } catch (Exception e) {
            e.printStackTrace();

        }
        obj.put("code",code);
        obj.put("msg",msg);
        return obj;
    }

    /**
     * 设备编辑
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/userList")
    public String userList(ModelMap model) throws Exception{
        try {
            return "view/device/user_list";
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 下发列表
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/getList")
    @ResponseBody
    public JSONObject getList(int currentPage, int nums, String userName, String deviceName, String orgId, String state) throws Exception {
        SysUser user = getUser();
        JSONObject obj = new JSONObject();
        int code = 200;
        String msg = "查询成功！";
        try {
            Map map = new HashMap();
            map.put("minNum", (currentPage-1)*nums);
            map.put("maxNum", nums);
            if(Utils.isNotNull(deviceName)){
                map.put("deviceName", "%"+deviceName+"%");
            }
            if(Utils.isNotNull(userName)){
                map.put("userName", "%"+userName+"%");
            }
            map.put("orgId", orgId);
            map.put("isIssued", state);
            List<Map<String, Object>> list = deviceService.getUserList(map);
            int total = deviceService.countUserList(map);

            obj.put("data",list);
            obj.put("count",total);
            obj.put("code",0);
            obj.put("msg","");
        } catch (Exception e) {
            e.printStackTrace();
            obj.put("code",9);
            obj.put("msg","查询异常");
        }

        return obj;
    }



}
